<?php

namespace longhu\base;

use longhu\exceptions\FailedException;

abstract class BaseDao
{

    abstract public function setModel():string;
    public function getModel():BaseModel
    {
        if($this->setModel()){
            return app()->make($this->setModel());
        }
        throw new FailedException("model is null");
    }

    /**
     * 创建
     * @param array $data
     * @return bool
     */
    public function create(array $data)
    {
        return $this->getModel()->storeBy($data);
    }

    /**
     * 修改
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateById(int $id,array $data)
    {
        return $this->getModel()->updateBy($id,$data);
    }


    /**
     * 删除
     * @param int $id
     * @param bool $force
     * @return mixed
     */
    public function deleteBy(int $id, bool $force = false)
    {
        return $this->getModel()->deleteBy($id,$force);
    }

    public function getLists(array $search = [],array $with = [],int $page = 0,int $limit = 0)
    {

    }

    public function getPageLists(array $search = [],$field = '',$with = [])
    {
        if(empty($field)){
            $field = join(',',$this->getModel()->getFields());
        }
        return $this->getModel()
            ->catchSearch($search)
            ->when(!empty($with),function ($query) use ($with){
                $query->with($with);
            })
            ->field($field)
            ->paginate();

    }

    public function getInfo(int $id)
    {
        return $this->getModel()->findBy($id);
    }
}